package com.leo.customer.controller

import com.leo.annotation.UserLoginToken
import com.leo.customer.entity.pojo.Organize
import com.leo.customer.service.OrganizeService
import com.leo.customer.service.UserService
import com.leo.customer.utils.Utils
import com.leo.customer.utils.getUser
import com.leo.result.ResultCode
import com.leo.result.ResultEntity
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.web.bind.annotation.*
import javax.servlet.http.HttpServletRequest

@Api("组织接口管理", tags = ["组织操作接口"])
@RequestMapping("organize")
@RestController
class OrganizeController {


    @Autowired
    lateinit var organizeService: OrganizeService

    @Autowired
    lateinit var userService: UserService

    @ApiOperation("创建组织")
    @UserLoginToken
    @PostMapping("createOrganize")
    fun createOrUpdateOrganize(request:HttpServletRequest, @RequestBody organize: Organize) :ResultEntity<Organize> {
        val user = request.getUser()
        if (!user.hasAuthenticate()){
            return ResultEntity.fail(ResultCode.NotAuthenticate)
        }
        val createOrEditOrganize = organizeService.createOrEditOrganize(organize)
        if (createOrEditOrganize != null){
            return ResultEntity.success(createOrEditOrganize)
        }
        return ResultEntity.fail(ResultCode.CreateOrganizeFail)
    }

    @ApiOperation(value = "根据Id获取组织信息", tags = ["根据Id获取组织信息"])
    @GetMapping("getOrganizeById")
    fun getOrganizeInfo(request: HttpServletRequest, @RequestParam id:Int) : ResultEntity<Organize>{
        val organize = organizeService.getOrganize(id)
        return if (organize == null){
            ResultEntity.fail("未找到对应组织")
        }else{
            ResultEntity.success(organize)
        }
    }

    @GetMapping("organizeList")
    fun getOrganizeList(request: HttpServletRequest, @RequestParam page:Int,@RequestParam size:Int): Page<Organize> {
        return organizeService.organizeList(page, size)
    }

}
